﻿<%@ WebHandler Language="C#" Class="ImgProfile" %>

using System;
using System.Web;
using System.Linq;

public class ImgProfile : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        bool valid = false;

        if (context.User.Identity.IsAuthenticated)
        {
            valid = true;
            string user = AHMIDEAFunctions.ContextUser(context);

            using (DC_AHMIDEADataContext dc = new DC_AHMIDEADataContext())
            {
                var p = dc.AHMIDEA_MSTPROFILEs.FirstOrDefault(i => i.vuserid == user);
                if ((p != null) && (!string.IsNullOrEmpty(p.vtype)))
                {
                    string ext = AHMIDEAFunctions.GetDefaultExtension(p.vtype);

                    context.Response.ContentType = p.vtype;
                    context.Response.BinaryWrite(p.bimage.ToArray());
                    context.Response.Flush();
                }
                else
                {
                    valid = false;
                }
            }
        }
        else {
            valid = false;
        }
        if (!valid) {
            context.Response.ContentType = "image/jpeg";
            byte[] defImg = System.IO.File.ReadAllBytes(context.Server.MapPath("/") + "assets/images/user.jpg");
            context.Response.BinaryWrite(defImg);
            context.Response.Flush();
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}